package com.lujianfei.powersearch.utils

import android.graphics.Color
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.util.Patterns
import android.view.View
import android.widget.TextView
import com.lujianfei.powersearch.logger.LoggerFactory
import java.util.regex.Pattern

class SpanUtils {
    private val TAG = "SpanUtils"
    private var spannableString:SpannableString?= null
    private var textView:TextView?= null
    private val log = LoggerFactory.getLogger()

    fun textView(textView: TextView?):SpanUtils {
        this.textView = textView
        return this
    }
    fun content(content:String):SpanUtils {
        if (spannableString == null) {
            spannableString = SpannableString(content)
        }
        return this
    }

    fun addUrl(onLinkCallback:(String)->Unit):SpanUtils {
        spannableString?.let {
            val matcher = Patterns.WEB_URL.matcher(it.toString())
            while(matcher.find()) {
                val start = matcher.start()
                val end = matcher.end()
                spannableString?.setSpan(ForegroundColorSpan(Color.BLUE), start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                spannableString?.setSpan(CustomClickableSpan(matcher.group(), onLinkCallback = onLinkCallback), start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            textView?.text = it
            textView?.movementMethod = LinkMovementMethod.getInstance()
        }
        return this
    }

    fun addPhone(onLinkCallback:(String)->Unit):SpanUtils {
        spannableString?.let {
            val phonePattern = Pattern.compile("1[3-8]\\d{9}")
            val wholeString = it.toString()
            val matcher = phonePattern.matcher(wholeString)
            while(matcher.find()) {
                val start = matcher.start()
                val end = matcher.end()
                var rightNoNumber = true
                if (start > 0) {
                    val leftNoNumber = !wholeString[start-1].isDigit()
                    if (end < wholeString.length - 1) { // 不在结尾
                        rightNoNumber = !wholeString[end + 1].isDigit()
                    }
                    if (leftNoNumber && rightNoNumber) { // 判断两侧有没有数字
                        spannableString?.setSpan(ForegroundColorSpan(Color.BLUE), start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                        spannableString?.setSpan(CustomClickableSpan(matcher.group(), onLinkCallback = onLinkCallback), start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }
                } else {
                    if (end < wholeString.length - 1) { // 不在结尾
                        rightNoNumber = !wholeString[end + 1].isDigit()
                    }
                    if (rightNoNumber) { // 判断右侧有没有数字
                        spannableString?.setSpan(
                            ForegroundColorSpan(Color.BLUE),
                            start,
                            end,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                        spannableString?.setSpan(
                            CustomClickableSpan(
                                matcher.group(),
                                onLinkCallback = onLinkCallback
                            ), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                    }
                }
            }
            textView?.text = it
            textView?.movementMethod = LinkMovementMethod.getInstance()
        }
        return this
    }

    fun addAddress(onLinkCallback: (String) -> Unit): SpanUtils {
        spannableString?.let {
            val addressPattern = Pattern.compile("地址[:：](\\S+)")
            val matcher = addressPattern.matcher(it.toString())
            while (matcher.find()) {
                val start = matcher.start()
                val end = matcher.end()
                spannableString?.setSpan(ForegroundColorSpan(Color.GREEN), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                spannableString?.setSpan(CustomClickableSpan(matcher.group(1), onLinkCallback = onLinkCallback), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            textView?.text = it
            textView?.movementMethod = LinkMovementMethod.getInstance()
        }
        return this
    }

    class CustomClickableSpan(val url:String,val onLinkCallback:(String)->Unit): ClickableSpan() {
        override fun onClick(v: View) {
            onLinkCallback.invoke(url)
        }
    }
}